package com.erp.erp_ui.ProductFjGe;

import com.erp.erp_entitys.ProductFjGe.ProductFjGeEntity;
import com.erp.erp_entitys.ProductFjGe.ProductFjGeExample;
import com.erp.erp_servers.ProductFjGe.IProductFjGeService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName OperationLogControl
 * @Description 产品附件信息
 * @Author songjizhen
 * @Date 2020-06-30 21:41
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "productfjge")
public class ProductFjGeControl extends Base {

    @Autowired
    IProductFjGeService productFjGeService;
    /**
     * 根据产编码、规格编码获取信息
     * @param
     * @return
     */
    @RequestMapping(value = "selectByPage")
    @ResponseBody
    public R<List<ProductFjGeEntity>> selectByPage(String zhuge,String mainProductCode){
        try{
            ProductFjGeExample example=new ProductFjGeExample();
            ProductFjGeExample.Criteria criteria = example.createCriteria();
            /*criteria.andProductCodeEqualTo(productCode);
            criteria.andFjCodeEqualTo(fjCode);*/
            criteria.andZhugeEqualTo(zhuge);
            criteria.andMainProductCodeEqualTo(mainProductCode);
            List<ProductFjGeEntity> productFjGeEntities = productFjGeService.selectByExample(example);
            return R.build(productFjGeEntities);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据产编码、规格编码获取对应配件信息
     * @param zhuge 产品编码
     * @param mainProductCode 规格编码
     * @return 对应配件信息
     */
    @RequestMapping(value = "selectProductFjGe")
    @ResponseBody
    public R<List<ProductFjGeEntity>> selectProductFjGe(String zhuge,String mainProductCode){
        try{
            ProductFjGeExample example=new ProductFjGeExample();
            ProductFjGeExample.Criteria criteria = example.createCriteria();
            criteria.andZhugeEqualTo(zhuge);
            criteria.andMainProductCodeEqualTo(mainProductCode);
            List<ProductFjGeEntity> productFjGeEntities = productFjGeService.selectByExample(example);
            return R.build(productFjGeEntities);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 批量删除规格型号
     * @param ids
     * @return
     */
    @RequestMapping(value = "delgexh",method = RequestMethod.POST)
    @ResponseBody
    public R<Integer> delgexh(String[] ids){
        try{
            List<Integer> list=new ArrayList<>();
            for(int i=0;i<ids.length;i++){
                list.add(Integer.parseInt(ids[i].replace("[","").replace("]","")));
            }
            ProductFjGeExample example=new ProductFjGeExample();
            ProductFjGeExample.Criteria criteria = example.createCriteria();
            criteria.andIdIn(list);
            int num = productFjGeService.deleteByExample(example);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 保存
     * @param pfge
     * @return
     */
    @RequestMapping(value = "savege",method = RequestMethod.POST)
    @ResponseBody
    public  R<Integer> savege(@RequestBody ProductFjGeEntity pfge){
        try{
            int num=0;
            if(pfge.getId() ==null){
                num=productFjGeService.insertSelective(pfge);
            }else{
               num= productFjGeService.updateByPrimaryKeySelective(pfge);
            }
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
}
